﻿#include "AppendCompound.h"
// core
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
//framework
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
//python
#include "FITK_Kernel/FITKPython/FITKPythonInterface.h"
//operator
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/ParaWidgetInterfaceOperator.h"
//interface
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoCommandList.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperSolid.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelSphere.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoMultiDelete.h"
//component
#include "FITK_Component/FITKGeoCompOCC/FITKOCCModelSolid.h"
#include "FITK_Component/FITKGeoCompOCC/FITKOCCModelOperCompound.h"
#include <QMetaEnum>
#include <QDebug>

AppendCompound::AppendCompound(int id /*= 0*/)
{
    if (id == 0) return;
    m_command = wTools.getGeoCommand(Interface::FITKGeoEnum::FITKGeometryComType::FGTCompoundAppendShape);
    if (!m_command) wTools.updateExecutionStatus(false);
    else m_appendCompound = dynamic_cast<Interface::FITKAbsGeoModelOperCompound*>(m_command);
}

AppendCompound::AppendCompound(Interface::FITKAbsGeoModelOperCompound* AppendCompound)
{
    m_command = m_appendCompound = AppendCompound;
}

const QString AppendCompound::getClassName()
{
    return "AppendCompound";
}

bool AppendCompound::setInputCmdId(int cmdId)
{
    if (!m_appendCompound) return false;
    m_appendCompound->setInputCmdId(Interface::VirtualShape(cmdId, -1));
    return true;
}

bool AppendCompound::setShapeCmdIds(QList<int> cmdIds)
{
    if (!m_appendCompound) return false;
    QList<Interface::VirtualShape> vShapes;
    for (int i = 0; i < cmdIds.size(); i++)
    {
        vShapes.append(Interface::VirtualShape(cmdIds[i], -1));
    }
    m_appendCompound->setShapeCmdIds(vShapes);
    return true;
}

AppendCompound* AppendCompoundWrapper::new_AppendCompound()
{
    return new AppendCompound(-1);
}

void AppendCompoundWrapper::delete_AppendCompound(AppendCompound* obj)
{
    if (obj) delete obj;
}

void AppendCompoundWrapper::setInputCmdName(AppendCompound* obj, const QString& name)
{
    auto data = geoCmdUndoList->getDataByName(name);
    bool flag = data;
    if (data) obj->setInputCmdId(data->getDataObjectID());
    wTools.updateExecutionStatus(flag);
}

void AppendCompoundWrapper::setAppendCmdName(AppendCompound * obj, const QString& name)
{
    auto data = geoCmdUndoList->getDataByName(name);
    bool flag = data;
    QList<int> vShapeIds;
    if (flag)
    {
        vShapeIds.append(data->getDataObjectID());
        if (flag) obj->setShapeCmdIds(vShapeIds);
    }
    wTools.updateExecutionStatus(flag);
}

void AppendCompoundWrapper::update(AppendCompound* obj)
{
    auto appendCompound = dynamic_cast<Interface::FITKAbsGeoModelOperCompound*>(obj->getCommand());

    // 执行操作器刷新命令。
    auto m_Operator = FITKOPERREPO->getOperatorT<EventOper::ParaWidgetInterfaceOper>("actionCompoundAppendShape");

    // 传递输入参数。
    auto vShapes = appendCompound->getShapeCmdIds();
    auto compoundVShape = appendCompound->getInputCmdId();
    m_Operator->setArgs("CompoundVShape", QVariant::fromValue(compoundVShape));
    m_Operator->setArgs("VShapes", QVariant::fromValue(vShapes));

    // 执行功能。
    bool flag = m_Operator->execProfession();
    wTools.updateExecutionStatus(flag);

    // 获取数据ID。
    int histId = -1;
    if (flag && m_Operator->argValue("Id", histId))
        registerSig(obj, histId, "delete");
}

AppendCompound* AppendCompoundWrapper::static_AppendCompound_Create()
{
    return new_AppendCompound();
}
